package com.cswnode.demo.controller;

import com.cswnode.demo.dao.ClassRoomDao;
import com.cswnode.demo.entity.ClassRoom;
import com.cswnode.demo.entity.Page;
import com.cswnode.demo.service.ClassRoomService;
import com.cswnode.demo.util.ResponseVo;
import com.cswnode.demo.vo.ClassRoomVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author csw
 * @create 2021/3/9 12:29
 */
@Api(tags = "班级管理")
@RestController
@RequestMapping("/classRoom")
public class ClassRoomController {

    @Autowired
    private ClassRoomService classRoomService;
    @Autowired
    private ClassRoomDao classRoomDao;

    @ApiOperation(value = "添加班级")
    @PostMapping("/add")
    public ResponseVo add(@RequestBody ClassRoom classRoom){
        if (classRoom == null){
            return ResponseVo.error("班级信息不得为空！");
        }
        ClassRoom insert = classRoomService.insert(classRoom);
        if (insert == null){
            return ResponseVo.error("添加失败！");
        }
        return ResponseVo.success("添加成功！");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/query")
    public ResponseVo query(@RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10")Integer limit, String className){
        Map<String,Object> result = new HashMap<>();
        Page page = new Page();
        page.setCurrent(current);
        page.setLimit(limit);
        page.setRows(classRoomService.selectNum(className));
        List<ClassRoomVo> ClassRoomVos = classRoomService.queryVoByLimit(page.getOffset(), page.getLimit(), className);
        result.put("page",page);
        result.put("ClassRoomVos",ClassRoomVos);
        return ResponseVo.success(result);
    }

    @ApiOperation(value = "查找所有班级")
    @GetMapping("/queryAll")
    public ResponseVo queryAll(){
        List<ClassRoomVo> classRoomVos = classRoomDao.queryOptions();
        return ResponseVo.success(classRoomVos);
    }

    @ApiOperation(value = "修改班级")
    @PostMapping("/update")
    public ResponseVo update(@RequestBody ClassRoom classRoom){
        if (ObjectUtils.isEmpty(classRoom)){
            return  ResponseVo.error("专业信息不得为空！");
        }
        ClassRoom update = classRoomService.update(classRoom);
        if (update == null){
            return  ResponseVo.error("修改失败！");
        }
        return ResponseVo.success("修改成功！");
    }

    @ApiOperation(value = "删除专业")
    @GetMapping("/delete/{classId}")
    public ResponseVo delete (@PathVariable("classId")Long classId){
        if (classId == null){
            return ResponseVo.error("删除失败！");
        }
        boolean result = classRoomService.deleteById(classId);
        if (!result){
            return ResponseVo.error("删除失败");
        }
        return ResponseVo.success("删除成功");
    }

}
